package org.uni_hi.lsep.util;

import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.uni_hi.lsep.AppManager;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class FileManager {

	DocumentBuilder docBdr=null;

	public static String ROOT_TAG="simulationssettings",PACKETS_TAG="packets",PACKET_TAG="packet",RULES_TAG="rules",RULE_TAG="rule";
	
	public static final String NAME_ATT="name",DESCRIPTION_ATT="description",ACTION_ATT="action",
		DST_IP_ATT="dstip",SRC_IP_ATT="srcip",FW_IP_ATT="fwip",
		SRC_PORT_ATT="srcport",DST_PORT_ATT="dstport",
		ICMP_TYPE_ATT="icmptype",ICMP_CODE_ATT="icmpcode";
	
	public FileManager(){
		try {
			docBdr=DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	public int loadSettingsFile(File file){
		AppManager.openFirewallSimulation();
		Document doc=null;
		try {
			doc=docBdr.parse(file);
			doc.normalizeDocument();
			NodeList nl = doc.getElementsByTagName(PACKET_TAG);
			AppManager.getMainFrame().getTestPanel().getAssignmentTabbedPane().addPackets(nl);
			nl=doc.getElementsByTagName(RULE_TAG);
			AppManager.getMainFrame().getTestPanel().getSimsettingsPanel().addTableRows(nl);
			AppManager.getMainFrame().getTestPanel().getAssignmentTabbedPane().setSelectedIndex(1);
			AppManager.getMainFrame().setStatus("Daten erfolgreich geladen");
			return 1;
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		AppManager.getMainFrame().setStatus("Daten konnten nicht geladen werden");
		return -1;
	}

	public int saveSettingsToFile(File selectedFile) {
		Document doc=docBdr.newDocument();
		try {
			Transformer trafo=TransformerFactory.newInstance().newTransformer();
			StreamResult result=new StreamResult(selectedFile);
			DOMSource dos=new DOMSource();
			doc.setXmlVersion("1.0");
			doc.setStrictErrorChecking(false);
			Element root=doc.createElement(ROOT_TAG);
			doc.appendChild(root);
			Node node=root.appendChild(doc.createElement(PACKETS_TAG));
			//Add the packets from the Simulation
			Vector<DataPacket> v=AppManager.getMainFrame().getTestPanel().getAssignmentTabbedPane().getPacketData();

			for(DataPacket dp:v){
				node.appendChild(dp.setElementData(doc.createElement(PACKET_TAG)));
			}

			node=root.appendChild(doc.createElement(RULES_TAG));
			//Add the rules from the Simulation
			v=AppManager.getMainFrame().getTestPanel().getSimsettingsPanel().getRuleRows();

			for(DataPacket dp:v){
				node.appendChild(dp.setElementData(doc.createElement(RULE_TAG)));
			}
			
			doc.normalizeDocument();
			dos.setNode(doc);
			trafo.transform(dos, result);
			return 1;
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (DOMException e) {
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
		return -1;
	}
}